package com.cmeduSystem.common.report.util;

import com.cmeduSystem.common.core.exception.MyRuntimeException;
import com.cmeduSystem.common.dbutil.provider.DataSourceProvider;
import com.cmeduSystem.common.dbutil.util.DataSourceUtil;
import com.cmeduSystem.common.report.model.ReportDblink;
import com.cmeduSystem.common.report.service.ReportDblinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 报表打印模块动态加载的数据源工具类。
 *
 * @author xulei
 * @date 2025-09-04
 */
@Slf4j
@Component
public class ReportDataSourceUtil extends DataSourceUtil {

    @Autowired
    private ReportDblinkService dblinkService;

    @Override
    public int getDblinkTypeByDblinkId(Long dblinkId) {
        DataSourceProvider provider = this.dblinkProviderMap.get(dblinkId);
        if (provider != null) {
            return provider.getDblinkType();
        }
        ReportDblink dblink = dblinkService.getById(dblinkId);
        if (dblink == null) {
            throw new MyRuntimeException("Report DblinkId [" + dblinkId + "] doesn't exist!");
        }
        this.dblinkProviderMap.put(dblinkId, this.getProvider(dblink.getDblinkType()));
        return dblink.getDblinkType();
    }

    @Override
    public String getDblinkConfigurationByDblinkId(Long dblinkId) {
        ReportDblink dblink = dblinkService.getById(dblinkId);
        if (dblink == null) {
            throw new MyRuntimeException("Report DblinkId [" + dblinkId + "] doesn't exist!");
        }
        return dblink.getConfiguration();
    }
}
